package cn.bdqn.action;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import cn.bdqn.pojo.Users;
import cn.bdqn.service.UsersService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("userAction")
public class UserAction extends ActionSupport {
	
	private Users user;
	//注入userservice
	@Autowired
	@Qualifier("userService")
	private UsersService  usersService;



	//验证
	@Override
	public void validate() {
		if(user.getUsercode()==null || user.getUsercode().length()==0){
			addFieldError("user.userCode", "用户编码不能为空！");
		}
		if(user.getPwd()==null || user.getPwd().length()==0){
			addFieldError("user.pwd", "用户密码不能为空！");	
		}
	}

	//登录
	public String login(){
		//解耦获取session，request
		ActionContext  context = ActionContext.getContext();
		Map  session = context.getSession();
		Map  request = (Map) context.get("request");

		Users u = usersService.doLogin(user.getUsercode(),user.getPwd());
		if(u!=null){
			session.put("user", user);
			return  SUCCESS;
		}else {
			request.put("error","用户编号和密码不匹配！！");
			return  ERROR;
		}

	}


	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}





}
